package cn.yhp.mybatis;

import java.lang.reflect.Proxy;
import java.util.Map;

/**
 * @author yhp
 * @description 映射代理工厂
 * @date 2023/11/5
 */
public class MapperProxyFactory<T> {

    /**
     * 需要代理的接口
     */
    private Class<T> mapperInterface;

    public MapperProxyFactory(Class<T> mapperInterface){
        this.mapperInterface=mapperInterface;
    }

    /**
     * 创建mapperInterface的代理对象
     * @param sqlSession
     * @return
     */
    public T newInstance(Map<String,String> sqlSession){
        MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface);
        return (T)Proxy.newProxyInstance(mapperInterface.getClassLoader(),new Class[]{mapperInterface},mapperProxy);
    }
}
